package com.jiangruan.javaproject.net;
import java.io.*;
import java.net.*;
import java.util.Map;
import java.util.Set;

/**
 * @author:liuzuoqiang
 * @email:2639571477@qq.com
 * @desc:
 * @datetime: 2021/12/24 22:07
 */

public class HttpUtils {
    /**
     * 该函数用于实现get请求，并将请求的结果进行返回
     * @param path 请求的资源路径
     * @return 请求的结果
     */
    public static String get(String path){

        String line = "";//最开始空行
        String resulrt = "";//每次读取到以后都要累计起来读取结束

        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;

        try {
            //Union Resource Location 统一资源定位
            URL url = new URL(path);
            URLConnection conn = url.openConnection();
            //进行链接
            conn.connect();

            inputStream = conn.getInputStream();

            //字符流：按行读
            inputStreamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(inputStreamReader);

            while ((line = reader.readLine()) != null) {
                resulrt += line;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭读取流
            try {
                if (reader != null) {
                    reader.close();
                }
                if (inputStreamReader != null) {
                    inputStreamReader.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return resulrt;
    }

    /**
     * 封装java中的post原生请求，并返回请求结果
     *
     * @param path    请求的路径
     * @param header  请求的头设置
     * @param date     要发送的请求数据
     * @return   请求的结果
     */

    public static String post(String path,Map<String,String> header,byte[] date){
        OutputStream outputStream = null;
        InputStream inputStream = null;
        BufferedReader reader = null;
        String result = "";

        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");//设置请求类型
            conn.setDoOutput(true);//设置可以向外发送数据
            conn.setDoInput(true);//设置可以读取数据
            //设置请求头信息
            //ContentType:application/json
            //Authorization:"Basic "+base64(xxx)
            //如何读取map数据？
            //集合:
            //List:有序, for循环,foreach
            //Set: 无序, foreach, 迭代器
            //Map:无序， 键值对, String value = get(Key)
            if(!header.isEmpty()){
                Set<String> keys = header.keySet();
                for (String key: keys) {
                    String value = header.get(key);
                    conn.setRequestProperty(key, value);
                }
            }
            //进行连接
            conn.connect();

            //将数据发送出去
          outputStream= conn.getOutputStream();
          outputStream.write(date);

          //接受响应数据
            inputStream = conn.getInputStream();
            //IO流
            //输入流: 字节输入流(InputStream)     字节读取流(Reader)
            //输出流:  字节输出流(OutputStream)    字节写入流(Writer)
            //转换流:
            //InputStreamReader
            //OutputStreamWriter
            reader = new BufferedReader(new InputStreamReader(inputStream));
            String line ="";
            while ((line = reader.readLine())!=null);{
                result += line;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(reader!=null){
                    reader.close();
                }
                if(inputStream!=null){
                    outputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return result;
    }
}





